package ipgather.core.host.thread;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.apache.http.impl.client.CloseableHttpClient;

import ipgather.constant.PortConstant;
import ipgather.core.ip.qqwry.IPLocation;
import ipgather.core.ip.qqwry.IPSeeker;
import ipgather.core.proxy.thread.ProxyCheck;
import ipgather.core.redis.RedisDbAdapter;
import ipgather.entity.Host;

public class HostProductor extends Thread{
	
	private SyncStackHost stackHost;
	private Random random = new Random();
	private CloseableHttpClient httpClient;
	
	public HostProductor(final SyncStackHost stackHost,CloseableHttpClient httpClient) {
		this.stackHost = stackHost;
		this.httpClient = httpClient;
	}
	
	static long count = 0;

	@Override
	public void run() {
		final ProxyCheck proxyCheck = new ProxyCheck(httpClient);
		while(true){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(count%1000==0){
				System.out.println("查询总数:"+count);
			}
			new Thread(new Runnable() {
				@Override
				public void run() {
					StringBuffer sb = new StringBuffer(7);
					sb.append(random.nextInt(256)+1).append(".").append(random.nextInt(255)).append(".").append(random.nextInt(255)).append(".").append(random.nextInt(255));
					count++;
					while(true){
						String host = sb.toString();
						for (int port : PortConstant.PORTS) {//搜索常用服务器http端口
							if(proxyCheck.checkProxyHost(new Host(host,port))){
								System.err.println(new StringBuffer("有效[ ").append(host).append(" : ").append(port).append(" ]").toString());
								RedisDbAdapter.create().saveHost(host, port);// 保存到redis数据库
							}
						}
					}
				}
			}).start();
		}
	}
	
}
